import { getCurrentUserId } from '@/utils/backend/auth'
import { prisma } from '@/utils/backend/db'
import { HttpStatus } from '@/utils/httpStatus'
import { NextResponse } from 'next/server'

export async function POST(req: Request, { params }: { params: { id: string } }) {
	try {
		const token = req.headers.get('Authorization')?.replace('Bearer ', '')!
		const currentUserId = await getCurrentUserId(token)
		const chatId = Number(params.id)

		// 更新所有对方发送的未读消息状态为已读
		await prisma.privateMessage.updateMany({
			where: {
				chatId,
				NOT: {
					senderId: currentUserId,
				},
				status: 'sent',
			},
			data: {
				status: 'read',
			},
		})

		return NextResponse.json({
			success: true,
			message: '消息状态更新成功',
		})
	} catch (error) {
		return NextResponse.json(
			{
				success: false,
				message: '消息状态更新失败',
			},
			{ status: HttpStatus.INTERNAL_SERVER_ERROR }
		)
	}
}
